package frobagroup.controller.frontEnd;


import com.google.common.collect.ImmutableMap;
import frobagroup.entities.common.Credential;
import frobagroup.entities.frontEnd.User;
import frobagroup.entities.frontEnd.UserProfile;
import frobagroup.services.frontEnd.UserLoginService;
import frobagroup.utils.Constants;
import frobagroup.utils.JwtUtil;
import frobagroup.utils.MD5;
import frobagroup.utils.ResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Created by Tinsley on 2016/7/19.
 */
@RequestMapping("/webAPI/unAuth")
@RestController
public class UserLoginController {

    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private UserLoginService userLoginService;

    @ModelAttribute("data")
    public Map<String,Object> findStudent(@RequestBody Credential credential)
        throws ServletException {
        User user = userLoginService.findByEmailOrUsername(credential.getEmail(),
                credential.getUsername());
        Map<String,Object> map = new HashMap<>();
        map.put("requestData",new User(credential.getEmail(),credential.getPassword(),credential.getUsername()));
        map.put("reponseData",user);
        map.put("token",credential.getToken());
        return map;
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseEntity<ResponseModel<Object>> login(@ModelAttribute("data")Map<String,Object> data)
            throws ServletException {
        User user = (User)data.get("reponseData");
        if(Objects.isNull(user) || !(MD5.encode(((User)data.get("requestData")).getPassword()).equals(user.getPassword())))
            return ResponseModel.success(user, Constants.RESPONSE_STATUS_FAILURE);
        Map params = new HashMap();
        params.put("userId",user.getId());
        String token = jwtUtil.generateToken(params);
        userLoginService.lastLoginTime(user);
        UserProfile userProfile = userLoginService.findByUserId(user.getId());
        return ResponseModel.success(ImmutableMap.of("token",token,"userProfile",userProfile),
                Constants.RESPONSE_STATUS_SUCESS);
    }

    @RequestMapping(value = "/signUp", method = RequestMethod.POST)
    public ResponseEntity<ResponseModel<String>> signUp(@ModelAttribute("data")Map<String,Object> data)
            throws ServletException {

        if(Objects.nonNull(data.get("reponseData")))
            return ResponseModel.success(null, Constants.RESPONSE_STATUS_FAILURE);
        UserProfile up =  userLoginService.signUp((User)data.get("requestData"));
        Map p = new HashMap();
        p.put("userId",up.getUId());
        String token = jwtUtil.generateToken(p);
        return ResponseModel.success(token, Constants.RESPONSE_STATUS_SUCESS);

    }

    @RequestMapping(value = "/forgetPassword", method = RequestMethod.POST)
    public ResponseEntity<ResponseModel<UserProfile>> forgetPassword(@ModelAttribute("data")Map<String,User> data)
            throws ServletException {
//        Teacher teacher = data.get("reponseData");
//        if(Objects.nonNull(teacher)){
//            //        通过用户名可以找到数据，那么base64设置随机码并且发邮件，邮件点过去输入随机码匹配重新设置密码。
//            teaLoginService.forgetPassword(teacher);
//        }
//        //        如果数据不存在，则直接返回
        return ResponseModel.success(null,Constants.RESPONSE_STATUS_SUCESS);
    }


}
